const app = getApp();
var sliderWidth = 96;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    traineePhone: null,
    tabs: [{
      type: 'LOCALLY',
      text: '局部'
    }, {
      type: 'WHOLE',
      text: '整体'
    }],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    traineeId: null,
    positions: []
  },
  add: function (e) {
    const index = e.currentTarget.dataset.index;
    const positions = this.data.positions;
    const activeIndex = this.data.activeIndex;
    const changePosition = positions[index];
    let value = changePosition.currentValue + 1;
    if (value > 30) {
      value = 30;
    }
    if (activeIndex == 0) {
      changePosition.currentValue = value;
      changePosition[changePosition.columnName + '1'] = value;
    } else {
      changePosition.currentValue = value;
      changePosition[changePosition.columnName + '2'] = value;
    }
    this.setData({
      positions
    });
  },
  minus: function (e) {
    const index = e.currentTarget.dataset.index;
    const positions = this.data.positions;
    const activeIndex = this.data.activeIndex;
    const changePosition = positions[index];
    const minusOne = changePosition.currentValue - 1;
    const value = minusOne > 0 ? minusOne : 0;
    if (activeIndex == 0) {
      changePosition.currentValue = value;
      changePosition[changePosition.columnName + '1'] = value;
    } else {
      changePosition.currentValue = value;
      changePosition[changePosition.columnName + '2'] = value;
    }
    this.setData({
      positions
    });
  },
  reset: function () {
    const positions = this.data.positions;
    const activeIndex = this.data.activeIndex;
    positions.forEach(function (item) {
      item.currentValue = 0;
      if (activeIndex == 0) {
        item[item.columnName + '1'] = 0;
      } else {
        item[item.columnName + '2'] = 0;
      }
    });
    this.setData({
      positions
    });
  },
  addAll: function () {
    const positions = this.data.positions;
    const activeIndex = this.data.activeIndex;
    positions.forEach(function (item) {
      let value = item.currentValue + 1;
      if (value > 30) {
        value = 30;
      }
      item.currentValue = value;
      if (activeIndex == 0) {
        item[item.columnName + '1'] = value;
      } else {
        item[item.columnName + '2'] = value;
      }
    });
    this.setData({
      positions
    });
  },
  minusAll: function () {
    const positions = this.data.positions;
    const activeIndex = this.data.activeIndex;
    positions.forEach(function (item) {
      const minusOne = item.currentValue - 1;
      const value = minusOne > 0 ? minusOne : 0;
      item.currentValue = value;
      if (activeIndex == 0) {
        item[item.columnName + '1'] = value;
      } else {
        item[item.columnName + '2'] = value;
      }
    });
    this.setData({
      positions
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const traineePhone = wx.getStorageSync("traineePhone");
    this.setData({
      traineePhone
    });
    const positions = app.globalData.emsItems;
    const url = app.globalData.url;
    const that = this;
    wx.request({
      url: url + '/train/emsAssessment/' + traineePhone,
      success: function (response) {
        if (response.data.success) {
          const emsObj = response.data.content;
          positions.forEach(function (position) {
            switch (position.columnName) {
              case 'ertou':
                position.currentValue = emsObj.ertou1;
                position.ertou1 = emsObj.ertou1;
                position.ertou2 = emsObj.ertou2;
                break;
              case 'santou':
                position.currentValue = emsObj.santou1;
                position.santou1 = emsObj.santou1;
                position.santou2 = emsObj.santou2;
                break;
              case 'xiongbu':
                position.currentValue = emsObj.xiongbu1;
                position.xiongbu1 = emsObj.xiongbu1;
                position.xiongbu2 = emsObj.xiongbu2;
                break;
              case 'fubu':
                position.currentValue = emsObj.fubu1;
                position.fubu1 = emsObj.fubu1;
                position.fubu2 = emsObj.fubu2;
                break;
              case 'shangbei':
                position.currentValue = emsObj.shangbei1;
                position.shangbei1 = emsObj.shangbei1;
                position.shangbei2 = emsObj.shangbei2;
                break;
              case 'beikuo':
                position.currentValue = emsObj.beikuo1;
                position.beikuo1 = emsObj.beikuo1;
                position.beikuo2 = emsObj.beikuo2;
                break;
              case 'xiabei':
                position.currentValue = emsObj.xiabei1;
                position.xiabei1 = emsObj.xiabei1;
                position.xiabei2 = emsObj.xiabei2;
                break;
              case 'tunbu':
                position.currentValue = emsObj.tunbu1;
                position.tunbu1 = emsObj.tunbu1;
                position.tunbu2 = emsObj.tunbu2;
                break;
              case 'qiantui':
                position.currentValue = emsObj.qiantui1;
                position.qiantui1 = emsObj.qiantui1;
                position.qiantui2 = emsObj.qiantui2;
                break;
              case 'houtui':
                position.currentValue = emsObj.houtui1;
                position.houtui1 = emsObj.houtui1;
                position.houtui2 = emsObj.houtui2;
                break;
            }
          });
          that.setData({
            positions
          });
        }
      }
    })
    wx.setNavigationBarTitle({
      title: 'EMS评估-局部'
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  tabClick: function (e) {
    const id = e.currentTarget.id;
    const text = id == 0 ? 'EMS评估-局部' : 'EMS评估-整体';
    const positions = this.data.positions;
    wx.setNavigationBarTitle({
      title: text
    });
    // const bgColor = id == 0 ? "#333333" : "#4d3a92";
    // wx.setNavigationBarColor({
    //   frontColor: '#ffffff',
    //   backgroundColor: bgColor
    // })
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: id
    });
    if (id == 0) {
      positions.forEach(function (position) {
        switch (position.columnName) {
          case 'ertou':
            position.currentValue = position.ertou1;
            break;
          case 'santou':
            position.currentValue = position.santou1;
            break;
          case 'xiongbu':
            position.currentValue = position.xiongbu1;
            break;
          case 'fubu':
            position.currentValue = position.fubu1;
            break;
          case 'shangbei':
            position.currentValue = position.shangbei1;
            break;
          case 'beikuo':
            position.currentValue = position.beikuo1;
            break;
          case 'xiabei':
            position.currentValue = position.xiabei1;
            break;
          case 'tunbu':
            position.currentValue = position.tunbu1;
            break;
          case 'qiantui':
            position.currentValue = position.qiantui1;
            break;
          case 'houtui':
            position.currentValue = position.houtui1;
            break;
        }
      });
      this.setData({
        positions
      });
    } else if (id == 1) {
      positions.forEach(function (position) {
        switch (position.columnName) {
          case 'ertou':
            position.currentValue = position.ertou2;
            break;
          case 'santou':
            position.currentValue = position.santou2;
            break;
          case 'xiongbu':
            position.currentValue = position.xiongbu2;
            break;
          case 'fubu':
            position.currentValue = position.fubu2;
            break;
          case 'shangbei':
            position.currentValue = position.shangbei2;
            break;
          case 'beikuo':
            position.currentValue = position.beikuo2;
            break;
          case 'xiabei':
            position.currentValue = position.xiabei2;
            break;
          case 'tunbu':
            position.currentValue = position.tunbu2;
            break;
          case 'qiantui':
            position.currentValue = position.qiantui2;
            break;
          case 'houtui':
            position.currentValue = position.houtui2;
            break;
        }
      });
      this.setData({
        positions
      });
    }
  },
  save: function () {
    const positions = this.data.positions;
    const traineePhone = this.data.traineePhone;
    const url = app.globalData.url;
    let ertou1 = 0;
    let ertou2 = 0;
    let santou1 = 0;
    let santou2 = 0;
    let xiongbu1 = 0;
    let xiongbu2 = 0;
    let fubu1 = 0;
    let fubu2 = 0;
    let shangbei1 = 0;
    let shangbei2 = 0;
    let beikuo1 = 0;
    let beikuo2 = 0;
    let xiabei1 = 0;
    let xiabei2 = 0;
    let tunbu1 = 0;
    let tunbu2 = 0;
    let qiantui1 = 0;
    let qiantui2 = 0;
    let houtui1 = 0;
    let houtui2 = 0;
    positions.forEach(function (position) {
      switch (position.columnName) {
        case 'ertou':
          ertou1 = position.ertou1;
          ertou2 = position.ertou2;
          break;
        case 'santou':
          santou1 = position.santou1;
          santou2 = position.santou2;
          break;
        case 'xiongbu':
          xiongbu1 = position.xiongbu1;
          xiongbu2 = position.xiongbu2;
          break;
        case 'fubu':
          fubu1 = position.fubu1;
          fubu2 = position.fubu2;
          break;
        case 'shangbei':
          shangbei1 = position.shangbei1;
          shangbei2 = position.shangbei2;
          break;
        case 'beikuo':
          beikuo1 = position.beikuo1;
          beikuo2 = position.beikuo2;
          break;
        case 'xiabei':
          xiabei1 = position.xiabei1;
          xiabei2 = position.xiabei2;
          break;
        case 'tunbu':
          tunbu1 = position.tunbu1;
          tunbu2 = position.tunbu2;
          break;
        case 'qiantui':
          qiantui1 = position.qiantui1;
          qiantui2 = position.qiantui2;
          break;
        case 'houtui':
          houtui1 = position.houtui1;
          houtui2 = position.houtui2;
          break;
      }
    });
    wx.request({
      url: url + '/train/saveEmsAssessment',
      data: {
        phone: traineePhone,
        ertou1,
        ertou2,
        santou1,
        santou2,
        xiongbu1,
        xiongbu2,
        fubu1,
        fubu2,
        shangbei1,
        shangbei2,
        beikuo1,
        beikuo2,
        xiabei1,
        xiabei2,
        tunbu1,
        tunbu2,
        qiantui1,
        qiantui2,
        houtui1,
        houtui2
      },
      method: 'POST',
      success: function (response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          })
        }
      }
    })
  }
})